<?php


namespace app\manager\controller;


use app\extend\utils\JsonUtils;
use app\model\Admin;
use support\Request;

class AdminController extends CommonController
{
    public function adminCreate(Request $request)
    {
        $username = $request->post('username');
        $password = $request->post('password');
        $salt = create_noncestr(6); // 随机加密盐
        $password_encrypt = ucenter_encrypt_md5($password, $salt);
        try {
            $data = [
                'uuid' => create_uuid(),
                'username' => $username,
                'password' => $password_encrypt,
                'salt' => $salt,
                'status' => 1,
                'delete_time' => 0
            ];
            try {
                $result = Admin::create($data);
                return json(JsonUtils::jsonSuccess($result));
            } catch (\Exception $exception) {
                $data = [
                    'code' => $exception->getCode(),
                    'message' => $exception->getMessage()
                ];
                return json(JsonUtils::jsonErr(100001, '', $data));
            }
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }
    }

    public function adminUpdatePassword(Request $request)
    {
        $username = $request->post('password');
        $confirm_password = $request->post('confirm_password');

        $password_encrypt = ucenter_encrypt_md5($password, $salt);
        try {
            $data = [
                'uuid' => create_uuid(),
                'username' => $username,
                'password' => $password_encrypt,
                'salt' => $salt,
                'status' => 1,
                'delete_time' => 0
            ];
            try {
                $result = Admin::create($data);
                return json(JsonUtils::jsonSuccess($result));
            } catch (\Exception $exception) {
                $data = [
                    'code' => $exception->getCode(),
                    'message' => $exception->getMessage()
                ];
                return json(JsonUtils::jsonErr(100001, '', $data));
            }
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }
    }
}